package com.party.mobile.biz.sign;

import com.party.common.paging.Page;
import com.party.core.model.sign.GroupMember;
import com.party.core.model.sign.SignApplyStatus;
import com.party.core.model.sign.SignGradeStatus;
import com.party.core.service.sign.ISignApplyService;
import com.party.core.service.sign.biz.SignBusinessBizService;
import com.party.mobile.web.dto.login.output.CurrentUser;
import com.party.mobile.web.dto.sign.output.MyRankOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 签到成员业务接口
 * Created by yifeng
 *
 * @date 2018/3/5 0005
 * @time 下午 13:41
 */
@Service
public class SignMemberNewBizService {
    @Autowired
    private ISignApplyService signApplyService;
    @Autowired
    private SignBusinessBizService signBusinessBizService;

    /**
     * 个人排行
     *
     * @param groupMember 查询条件
     * @param page        分页参数
     * @return
     */
    public List<GroupMember> groupMemberList(GroupMember groupMember, Integer type, Page page) {
        Map<String, Object> params = signBusinessBizService.appendSearchParams(type);
        List<GroupMember> list = signApplyService.groupMemberListXcx(groupMember, params, page);
        return list;
    }

    /**
     * 个人成绩排行
     *
     * @param projectId 项目编号
     * @param type      类型
     * @param page      分页参数
     * @return 排名列表
     */
    public List<GroupMember> rankList(String projectId, Integer type, Page page) {
        Map<String, Object> params = signBusinessBizService.appendSearchParams(type);
        List<GroupMember> list = signApplyService.rankListXcx(projectId, params, page);
        return list;
    }

    /**
     * 我的排行
     *
     * @param projectId   项目编号
     * @param currentUser 当前用户
     * @return 我的排行信息
     */
    public MyRankOutput myProjectRank(String projectId, Integer type, CurrentUser currentUser) {
        MyRankOutput myRankOutput = null;
        if (null == currentUser) {
            return null;
        }

        GroupMember groupMember = signApplyService.get(currentUser.getId(), projectId);

        if (null != groupMember) {
            if (!SignApplyStatus.PASS_STATUS.getCode().equals(groupMember.getStatus())) {
                return null;
            }
            if (SignGradeStatus.INVALID.getCode().equals(groupMember.getGradeStatus())) {
                return null;
            }
            Map<String, Object> params = signBusinessBizService.appendSearchParams(type);
            params.put("memberId", currentUser.getId());
            List<GroupMember> list = signApplyService.rankListXcx(projectId, params, null);
            myRankOutput = MyRankOutput.transform(list.get(0));
        }
        return myRankOutput;
    }

    /**
     * 小组排行
     *
     * @param groupId     小组编号
     * @param currentUser 当前用户
     * @return 排行
     */
    public MyRankOutput myGroupRank(String groupId, Integer type, CurrentUser currentUser) {
        MyRankOutput myRankOutput = null;
        if (null == currentUser) {
            return null;
        }

        GroupMember groupMember = signApplyService.getByAuthorAndGroup(currentUser.getId(), groupId);
        if (null != groupMember) {
            if (!SignApplyStatus.PASS_STATUS.getCode().equals(groupMember.getStatus())) {
                return null;
            }
            Map<String, Object> params = signBusinessBizService.appendSearchParams(type);
            List<GroupMember> list = signApplyService.groupMemberListXcx(groupMember, params, null);
            myRankOutput = MyRankOutput.transform(list.get(0));
        }
        return myRankOutput;
    }
}
